home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / AmigaE / Src / Guide / csv.e < prev    next >
Text File  |  1995-06-18  |  2KB  |  73 lines

  1. /* Some constants for exceptions (ERR_NONE is zero: no error) */
  2. ENUM ERR_NONE, ERR_LEN, ERR_NEW, ERR_OPEN, ERR_READ
  3.  
  4. /* Make some exceptions automatic */
  5. RAISE ERR_LEN  IF FileLength()<=0,
  6.       ERR_NEW  IF New()=NIL,
  7.       ERR_OPEN IF Open()=NIL
  8.  
  9. PROC main() HANDLE
  10.   /* Note the careful initialisation of buffer and filehandle */
  11.   DEF buffer=NIL, filehandle=NIL, len, filename
  12.   filename:='datafile'
  13.   /* Get the length of data in the file */
  14.   len:=FileLength(filename)
  15.   /* Allocate just enough room for the data + a terminating NIL */
  16.   buffer:=New(len+1)
  17.   filehandle:=Open(filename, OLDFILE)
  18.   /* Read whole file, checking amount read */
  19.   IF len<>Read(filehandle, buffer, len) THEN Raise(ERR_READ)
  20.   /* Terminate buffer with a NIL just in case... */
  21.   buffer[len]:=NIL
  22.   process_buffer(buffer, len)
  23. EXCEPT DO
  24.   /* Both of these are safe thanks to the initialisations */
  25.   IF buffer THEN Dispose(buffer)
  26.   IF filehandle THEN Close(filehandle)
  27.   /* Report error (if there was one) */
  28.   SELECT exception
  29.   CASE ERR_LEN;   WriteF('Error: "\s" is an empty file\n', filename)
  30.   CASE ERR_NEW;   WriteF('Error: Insufficient memory to load file\n')
  31.   CASE ERR_OPEN;  WriteF('Error: Failed to open "\s"\n', filename)
  32.   CASE ERR_READ;  WriteF('Error: File reading error\n')
  33.   ENDSELECT
  34. ENDPROC
  35.  
  36. /* buffer is like a normal string since it's NIL-terminated */
  37. PROC process_buffer(buffer, len)
  38.   DEF start=0, end
  39.   REPEAT
  40.     /* Find the index of a linefeed after the start index */
  41.     end:=InStr(buffer, '\n', start)
  42.     /* If a linefeed was found then terminate with a NIL */
  43.     IF end<>-1 THEN buffer[end]:=NIL
  44.     process_record(buffer+start)
  45.     start:=end+1
  46.   /* We've finished if at the end or no more linefeeds */
  47.   UNTIL (start>=len) OR (end=-1)
  48. ENDPROC
  49.  
  50. PROC process_record(line)
  51.   DEF i=1, start=0, end, s
  52.   /* Show the whole line being processed */
  53.   WriteF('Processing record: "\s"\n', line)
  54.   REPEAT
  55.     /* Find the index of a comma after the start index */
  56.     end:=InStr(line, ',', start)
  57.     /* If a comma was found then terminate with a NIL */
  58.     IF end<>-1 THEN line[end]:=NIL
  59.     /* Point to the start of the field */
  60.     s:=line+start
  61.     IF s[]
  62.       /* At this point we could do something useful... */
  63.       WriteF('\t\d) "\s"\n', i, s)
  64.     ELSE
  65.       WriteF('\t\d) Empty Field\n', i)
  66.     ENDIF
  67.     /* The new start is after the end we found */
  68.     start:=end+1
  69.     INC i
  70.   /* Once a comma is not found we've finished */
  71.   UNTIL end=-1
  72. ENDPROC
  73.